;; -*- lexical-binding: t -*-

;; Some Common Settings

(set-register ?e '(file . "~/.emacs.d/"))
(set-register ?i '(file . "~/.emacs.d/.init/"))
(set-register ?h '(file . "~/repo/hexo-blog/"))
(set-register ?s '(file . "~/repo/hexo-blog/source/_posts/"))
(set-register ?r '(file . "~/repo/"))

;; 不闪
(setq-default ring-bell-function 'ignore)

;; 习惯使然 光标形状
(setq-default cursor-type 'bar)

;; 显示buffer的名字
(setq frame-title-format
      '("%b"
	(:eval
	 (if (buffer-file-name)
	     (format " (%s/)"
		     (directory-file-name
		      (file-name-directory
		       (abbreviate-file-name
			(buffer-file-name)))))
	   ""))
	" - Emacs rules!"))

;;  纠正org-mode的换号问题
(add-hook 'org-mode-hook
	  (lambda () (setq truncate-lines nil)))

(mapc (lambda (mode)
	(add-hook 'LaTeX-mode-hook mode))
      (list 'auto-fill-mode
	    'LaTeX-math-mode
	    ;; 'turn-on-reftex
	    ))

;; use ibuffer
(defalias #'list-buffers #'ibuffer)

(defalias #'-> #'thread-first)
(defalias #'->> #'thread-last)

;; (defalias #'dabbrev-expand #'hippie-expand)
(global-set-key [remap dabbrev-expand] 'hippie-expand)

(when (fboundp 'powerline-default-theme)
  (powerline-default-theme))

(windmove-default-keybindings)

(add-hook 'after-make-frame-functions
	  (lambda (&optional new-frame)
	    (let* ((fullscreen (list (assq 'fullscreen (frame-parameters))))
		   (alpha (list (assq 'alpha (frame-parameters)))))
	      (select-frame new-frame)
	      (modify-frame-parameters (selected-frame) fullscreen)
	      (modify-frame-parameters (selected-frame) alpha))))

;; For Mew.
;; (autoload 'mew "mew" nil t)
;; (autoload 'mew-send "mew" nil t)

(when (fboundp 'rainbow-delimiters-mode)
  (add-hook 'prog-mode-hook #'rainbow-delimiters-mode))

(use-package nyan-mode
  :defer t
  :config
  (nyan-mode 1))

(use-package auto-compile
  :defer t
  :config
  (auto-compile-on-load-mode 1)
  (auto-compile-on-save-mode 1))

(add-to-list 'auto-mode-alist '("\\.m\\'" . octave-mode))

;; (require 'wl)
(add-to-list 'auto-mode-alist '("/\\(?:neo\\)?mutt" . wl-draft-mode))
(autoload 'wl-draft-mode "wl" nil t)

(add-to-list 'load-path "~/.emacs.d/emacswiki")
(autoload 'toggle-corfu-english-helper "corfu-english-helper" nil t)

(unless (string-equal system-type "windows-nt")
  (require 'ls-lisp)
  (setq ls-lisp-dirs-first t)
  (setq ls-lisp-use-insert-directory-program nil))

(ignore-errors
  (require 'dired+)
  (require 'dired-sort-menu+)
  (when (featurep 'dired+)
    (add-to-list 'dired-compress-files-alist
		 '("\\.7z\\'" . "7z u -mmt %o %i"))
    (setcdr (assoc "\\.tar\\.zst\\'" dired-compress-files-alist)
	    ;; (concat (cdr (assoc "\\.tar\\.zst\\'" dired-compress-files-alist)) " -T0")
	    "tar -cf - %i | zstd -o %o -T0")))

(eval-when-compile
  (require 'tramp))
(setq vc-ignore-dir-regexp
      (format "\\(%s\\)\\|\\(%s\\)"
	      vc-ignore-dir-regexp
	      tramp-file-name-regexp))

;; (setq hippie-expand-try-functions-list
;;       '(
;;         try-expand-dabbrev
;;         ;; try-expand-dabbrev-from-kill
;;         try-complete-lisp-symbol-partially
;;         try-complete-lisp-symbol
;;         try-complete-file-name-partially
;;         try-complete-file-name
;;         try-expand-all-abbrevs
;;         try-expand-dabbrev-all-buffers
;;         ;; try-expand-list
;;         ;; try-expand-line
;;         ))

(add-hook 'js-mode-hook (lambda () (setq-local indent-tabs-mode nil)))

;; (eval-after-load 'google-translate-core
;;   '(setq google-translate-base-url "http://translate.google.cn/translate_a/single"
;;          google-translate-listen-url "http://translate.google.cn/translate_tts"))

;; (eval-after-load 'google-translate-tk
;;   '(setq google-translate--tkk-url "http://translate.google.cn/"))

;; (eval-after-load 'google-translate-smooth-ui
;;   '(setq google-translate-translation-directions-alist '(("en" . "zh-CN"))))

;; (eval-after-load 'google-translate-backend
;;   '(setq google-translate-backend-method 'curl))

(setq-default ispell-program-name (executable-find "hunspell"))

(transient-mark-mode)

(use-package fast-scroll
  :defer t
  :config
  (fast-scroll-config)
  (fast-scroll-mode 1))

;; (when (>= emacs-major-version 27)
;;   (with-eval-after-load 'org
;;     (dolist (face '(org-block
;;                     org-block-begin-line
;;                     org-block-end-line
;;                     org-level-1))
;;       (set-face-attribute face nil :extend t)))
;;   (with-eval-after-load 'magit
;;     (dolist (face '(magit-diff-hunk-heading
;;                     magit-diff-hunk-heading-highlight
;;                     magit-diff-hunk-heading-selection
;;                     magit-diff-hunk-region
;;                     magit-diff-lines-heading
;;                     magit-diff-lines-boundary
;;                     magit-diff-conflict-heading
;;                     magit-diff-added
;;                     magit-diff-removed
;;                     magit-diff-our
;;                     magit-diff-base
;;                     magit-diff-their
;;                     magit-diff-context
;;                     magit-diff-added-highlight
;;                     magit-diff-removed-highlight
;;                     magit-diff-our-highlight
;;                     magit-diff-base-highlight
;;                     magit-diff-their-highlight
;;                     magit-diff-context-highlight
;;                     magit-diff-whitespace-warning
;;                     magit-diffstat-added
;;                     magit-diffstat-removed
;;                     magit-section-heading
;;                     magit-section-heading-selection
;;                     magit-section-highlight
;;                     magit-section-secondary-heading
;;                     magit-diff-file-heading
;;                     magit-diff-file-heading-highlight
;;                     magit-diff-file-heading-selection))
;;       (set-face-attribute face nil :extend t)))
;;   (with-eval-after-load 'ediff
;;     (dolist (face '(ediff-current-diff-A
;;                     ediff-current-diff-Ancestor
;;                     ediff-current-diff-B
;;                     ediff-current-diff-C
;;                     ediff-even-diff-A
;;                     ediff-even-diff-Ancestor
;;                     ediff-even-diff-B
;;                     ediff-even-diff-C
;;                     ediff-fine-diff-A
;;                     ediff-fine-diff-Ancestor
;;                     ediff-fine-diff-B
;;                     ediff-fine-diff-C
;;                     ediff-odd-diff-A
;;                     ediff-odd-diff-Ancestor
;;                     ediff-odd-diff-B
;;                     ediff-odd-diff-C))
;;       (set-face-attribute face nil :extend t)))
;;   (with-eval-after-load 'hl-line
;;     (set-face-attribute 'hl-line nil :extend t))
;;   (set-face-attribute 'region nil :extend t))

(when (and
       (string-equal system-type "gnu/linux")
       ;; (getenv "WSLENV")
       (file-exists-p "/mnt/wslg/versions.txt")
       )
  ;; We are actually running GNU/Linux under MS-Windows’ WSL2 aka the GNU/kWindows.
  ;; (set-clipboard-coding-system 'gbk)
  ;; Work around copy failures.
  ;; (setq select-enable-clipboard t) not working.
  ;; Teach Emacs how to open links in your default Windows browser
  (let ((cmd-exe "/mnt/c/Windows/System32/cmd.exe")
        (cmd-args '("/c" "start")))
    (when (file-exists-p cmd-exe)
      (setq browse-url-generic-program  cmd-exe
            browse-url-generic-args     cmd-args
            browse-url-browser-function 'browse-url-generic
            search-web-default-browser 'browse-url-generic)))
  ;; (when (and (getenv "WAYLAND_DISPLAY") (not (equal (getenv "GDK_BACKEND") "x11")))
  ;; (setq
  ;;  interprogram-cut-function
  ;;  (lambda (text)
  ;;    ;; strangest thing: gui-select-text leads to gui-set-selection 'CLIPBOARD
  ;;    ;; text -- if I eval that with some string, it mostly lands on the wayland
  ;;    ;; clipboard, but not when it's invoked from this context.
  ;;    ;; (gui-set-selection 'CLIPBOARD text)
  ;;    ;; without the charset=utf-8 in type, emacs / wl-copy will crash when you paste emojis into a windows app
  ;;    (start-process "wl-copy" nil "wl-copy" "--trim-newline" "--type" "text/plain;charset=utf-8" text)))
  (set-register ?v '(file . "/mnt/c/Users/Kel/！Work/！！！投稿审稿资料/"))
  (set-register ?w '(file . "/mnt/c/Users/Kel/！Work/"))
  (setq-default ediff-window-setup-function #'ediff-setup-windows-multiframe)
  ;; (setq select-active-regions nil)
  )

(setq-default default-input-method "rime")

(setenv "PYTHONSTARTUP" (string-trim-right
			 (shell-command-to-string "python -m jedi repl")))
